package cn.itcast.usermanage.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;











import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;


@RequestMapping("/user")
@Controller
public class LoginController {
    @Autowired
    private UserService userService;
 
   
       
    @RequestMapping(value="/doregister",method = RequestMethod.POST)
    @ResponseBody
    public String doRegister(@RequestParam("username") String username,
            @RequestParam("password") String password) throws JsonProcessingException{
        System.out.println(username);
        System.out.println(password);
        Boolean bool = this.userService.saveUser(username,password);              
        if(bool){
             System.out.println("注册成功");
        }else{
            System.out.println("注册失败");
        }
        Map<String , Object> map = new LinkedHashMap<String , Object>();
        map.put("bool", bool);        
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.writeValueAsString(map);       
        return data;
    }
   
    @RequestMapping(value="/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public String doLogin(@RequestParam("username") String username,
                        @RequestParam("password") String password
                        ) throws JsonProcessingException{
        System.out.println(username+"---"+password);
        String token = this.userService.doLogin(username,password);
        System.out.println(token);
        if(null == token){
            token = "";
            System.out.println("登陸失敗");
        }else{
            System.out.println("登陸成功");
        }
        System.out.println(token);
        Map<String , Object> map = new LinkedHashMap<String , Object>();
        map.put("bool", token);        
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.writeValueAsString(map); 
        System.out.println(username);
        return data;
    }
}
